package com.device.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.device.Constant.BaseConstant;
import com.device.Entity.Lab;
import com.device.Entity.User;
import com.device.Exception.BaseException;
import com.device.Mapper.LabMapper;
import com.device.Mapper.UserMapper;
import com.device.Service.LabService;
import com.device.Service.UserService;
import com.device.common.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class LabServiceImpl extends ServiceImpl<LabMapper, Lab> implements LabService {
    @Resource
    private LabMapper labMapper;

    public void add(Lab lab) {
        LambdaQueryWrapper<Lab> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Lab::getName, lab.getName());
        Lab lab1 = labMapper.selectOne(lambdaQueryWrapper);
        if (lab1 != null) {
            throw new BaseException("名称已存在,请更换");
        }
        labMapper.insert(lab);


    }

    public void edit(Lab lab) {
        LambdaQueryWrapper<Lab> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Lab::getName, lab.getName());
        lambdaQueryWrapper.notIn(Lab::getId, lab.getId());
        Lab lab1 = labMapper.selectOne(lambdaQueryWrapper);
        if (lab1 != null) {
            throw new BaseException("名称已存在,请更换");
        }
        labMapper.updateById(lab);


    }

    public PageResult getpage(Integer pageNo, Integer pageSize, String keyword) {
        IPage<Lab> page = new Page<>(pageNo, pageSize);
        IPage<Lab> list = labMapper.page(page, keyword,null);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }
    public     PageResult getpage1(Integer uid, Integer pageNo, Integer pageSize, String keyword){
        IPage<Lab> page = new Page<>(pageNo, pageSize);
        IPage<Lab> list = labMapper.page(page, keyword,uid);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }
}
